package com.yurendao.shop.aspect.user;

import com.yurendao.shop.user.domain.User;
import com.yurendao.shop.user.service.exception.UserException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * Created by kong on 15/10/10.
 * 校验
 */
@Aspect
public class UserServiceValidate {

    @Before("execution(* com.yurendao.shop.user.service.impl.UserServiceImpl.insertUser(..))")
    public void insertUser(JoinPoint joinPoint) throws UserException{
        System.out.println("aop insert");
        if(joinPoint.getArgs()[0] instanceof User){
            User user = (User) joinPoint.getArgs()[0];
            if(user.getNickname()==null)
                throw new UserException("uname must not be null");
            int unamelength = user.getNickname().length();
            if(unamelength<3||unamelength>9){
                throw new UserException("uname string length must between 3 with 9");
            }
        }
    }
}
